package com.wk.wechattool;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.wk.wechattool.service.FloatingService;
import com.wk.wechattool.service.MyAccessibilityService;
import com.wk.wechattool.tool.BaseTool;


public class MainActivity extends Activity {
    private Context context ;
    private TextView showInfoTv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        context = this ;
        initView();
        startService(new Intent(MainActivity.this, FloatingService.class));
        startService(new Intent(MainActivity.this, MyAccessibilityService.class));
    }

    private void initView(){
        showInfoTv = findViewById(R.id.showInfoTv);
        findViewById(R.id.openSetting) .setOnClickListener(onClickListener);
        findViewById(R.id.openWchat).setOnClickListener(onClickListener);
        findViewById(R.id.exitBtn).setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id= view.getId();
            if(id==R.id.openSetting){
                Intent intent = new Intent( Settings.ACTION_ACCESSIBILITY_SETTINGS ) ;
                context.startActivity(intent) ;
            }

            else if(id==R.id.openWchat){
                Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
               if(intent!=null){
                   startActivity(intent);
               }else{
                   Toast.makeText(context ,"请先安装微信" ,Toast.LENGTH_SHORT).show();
               }

            }

            else if(id==R.id.exitBtn){
                ((Activity) context).finish();
                System.exit(0);
            }
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
        try {
            boolean  isOpen = BaseTool.checkAccessibilityEnabled_2(context ,MyAccessibilityService.class);
            String msg = isOpen ? "辅助权限已被允许" :"辅助权限已经关闭，请开启" ;
            if(isOpen){
                showInfoTv.setTextColor(Color.GREEN);
            }else{
                showInfoTv.setTextColor(Color.RED);
            }
            showInfoTv.setText(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 判断有没有悬浮窗权限
        boolean isCanOverlays =Settings.canDrawOverlays(this) ;
        if(!isCanOverlays){
            toastMsg("没有悬浮窗权限，请开启");
            BaseTool.requestSettingCanDrawOverlays(this ) ;
        }
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
            toastMsg("点击按钮退出");
            return true ;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void toastMsg(String msg){
        Toast.makeText(this , msg ,Toast.LENGTH_SHORT).show();
    }
}















